<template>
  <div class="app-container">
    <el-form class="searchForm" :inline="true" :model="queryForm" label-width="90px">
      <el-form-item label="名称">
        <el-input v-model="queryForm.dutyname"> </el-input>
      </el-form-item>
      <el-form-item label="编号">
        <el-input v-model="queryForm.dutycode"> </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="pageChange(1)">查询</el-button>
        <el-button type="info" @click="onAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      height="550"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column :resizable="false" label="操作" width="80" fixed>
        <template slot-scope="scope">
          <span class="edit-tooltip">
            <el-tooltip effect="dark" content="删除" placement="bottom">
              <el-button type="text" @click="onDel(scope.row)">
                <svg-icon style="fill: #ff4e1d" icon-class="butongyi"></svg-icon>
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="修改" placement="bottom">
              <el-button type="text" @click="onClickName(scope.row)">
                <svg-icon style="fill: #ff4e1d" icon-class="a-xiugai"></svg-icon>
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="审批记录" placement="bottom"> </el-tooltip>
          </span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" type="index" width="50" label="序号" align="center" />
      <el-table-column :resizable="false" prop="dutycode" label="编号" min-width="120" />
      <el-table-column :resizable="false" prop="dutyname" label="名称" min-width="120" />
      <el-table-column :resizable="false" prop="describe" label="备注" min-width="180" />
    </el-table>
    <el-pagination
      style="margin: 10px"
      :current-page="queryForm.pagenum"
      :page-sizes="[20, 50, 100]"
      :page-size="queryForm.pagesize"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
    <el-dialog
      destroy-on-close
      :title="title"
      :visible.sync="visible"
      :close-on-click-modal="false"
      width="600px"
      @close="onClose"
    >
      <span>
        <el-form ref="form" :model="form" :rules="formRules" label-width="100px" :inline="false">
          <el-form-item label="编号" prop="dutycode">
            <el-input v-model="form.dutycode" />
          </el-form-item>
          <el-form-item label="名称" prop="dutyname">
            <el-input v-model="form.dutyname" />
          </el-form-item>
          <el-form-item label="备注" prop="describe">
            <el-input
              v-model="form.describe"
              type="textarea"
              placeholder="请输入内容"
              :autosize="{ minRows: 1, maxRows: 4 }"
              resize="none"
              show-word-limit
            />
          </el-form-item>
        </el-form>
      </span>
      <div slot="footer">
        <el-button type="info" @click="onSubmit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { QueryPageList, Save, Del } from '@/api/settings/gangwei'

export default {
  name: 'Gangwei',
  data() {
    return {
      queryForm: {
        pagenum: 1,
        pagesize: 20
      },
      total: 0,
      tableData: [],
      loading: true,
      visible: false,
      title: '岗位信息',
      formRules: {
        dutycode: [{ required: true, message: '请输入编号', trigger: 'blur' }],
        dutyname: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      },
      form: {}
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    // 点击页面显示条数
    sizeChange(size) {
      this.queryForm.pagesize = size
      this.onSearch()
    },
    // 点击下一页与页码
    pageChange(page) {
      console.log(page)
      this.queryForm.pagenum = page
      this.onSearch()
    },
    // -------------------------------
    // 查询数据
    async onSearch() {
      this.loading = true
      const { content } = await QueryPageList(this.queryForm)
      this.tableData = content.list
      this.total = content.total
      this.loading = false
    },
    // 添加
    onAdd() {
      console.log(new Date())
      var time = new Date()
      this.form.time = time
      this.visible = true
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const res = await Save(this.form)
          if (res.success) {
            this.visible = false
            if (this.form.id) {
              this.$message({
                type: 'success',
                message: '修改数据成功'
              })
            } else {
              this.$message({
                type: 'success',
                message: '添加数据成功'
              })
            }
            this.onSearch()
          } else {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        } else {
          return false
        }
      })
    },
    onClickName(data) {
      this.visible = true
      this.form = data
    },

    onClose() {
      this.form = {}
    },
    onDel(data) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const del = await Del({ id: data.id })
          console.log(del)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.onSearch()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  height: 30px;
  line-height: 30px;
  background: rgba(0, 0, 0, 0.3);
  font-size: 14px;
  text-transform: uppercase;
  margin: 0 0 5px;
  padding: 0 10px;
  display: inline-block;
  letter-spacing: 0.5px;
}
</style>
